package com.smartrobot.smartrobot.circle;


import android.content.Context;
import android.os.Handler;
import android.os.Message;

import android.util.Log;
import com.smartrobot.smartrobot.Common.CommonFunc;
import com.smartrobot.smartrobot.DB.FriendListSqlHelper;
import com.smartrobot.smartrobot.DB.NewFriendSqlHelper;
import com.smartrobot.smartrobot.adapter.SocialListViewAdapter;
import com.smartrobot.smartrobot.circle.NewFriend.NewFriendUtil;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.pagerHolder.SocialCirlePagerHolder;
import com.smartrobot.smartrobot.pojo.NewFriendPojo;
import com.smartrobot.smartrobot.pojo.getFriendListPojo;

import com.smartrobot.smartrobot.util.FileUtil;
import com.smartrobot.smartrobot.util.User;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import java.util.HashMap;
//import java.util.Map;

///**
// * Created by Administrator on 2014/12/24.
// */
public class searchAndAddFriendResponse {



    //查找好友成功
 public static void searchFriendSuccess(Context context,JSONObject response,final Handler handler,Object object){
     try{
            String status = response.getString("status");
            if(status.equals(commonData.DATA_NORMAL)){
                JSONObject res = response.getJSONObject("res");
                getFriendListPojo userMsg = new getFriendListPojo();

                userMsg.setHeadImg(res.getString("u_head"));
                userMsg.setName(res.getString("u_nickname"));
                userMsg.setSex(res.getString("u_sex"));
                userMsg.setSign(res.getString("u_signature"));
                userMsg.setUserId(res.getString("u_id"));
                userMsg.setUserAccount(res.getString("u_phone"));
                userMsg.setRole(res.getString("u_role"));
                userMsg.setReName(res.getString("u_name"));

                Message message = new Message();
                message.what = commonData.SEARCH_FRIEND_SUCCESS_CODE;
                message.obj = userMsg;
                handler.sendMessage(message);
            }else if(status.equals("100")){
                Message message = new Message();
                message.arg1 = commonData.SEARCH_WITHOUT_LOGIN;
                message.what = commonData.USE_NOT_LOGIN_CODE;
                message.obj = commonData.USER_NOT_LOGIN;
                handler.sendMessage(message);
            }else if(status.equals("101")){
                Message message = new Message();
//                message.what = commonData.USER_NO_EXIST_CODE;
                message.what  = commonData.SHOW_TIP_CODE;
                message.obj = commonData.USER_NO_EXIST;
                handler.sendMessage(message);
            }else if(status.equals("102")){
                Message message = new Message();
//                message.what = commonData.PARAM_ERROR_CODE;
                message.what  = commonData.SHOW_TIP_CODE;
                message.obj = commonData.PARAM_ERROR;
                handler.sendMessage(message);
            }else if(status.equals("103")){
                Message message = new Message();
//                message.what = commonData.SERVER_ERROR_CODE;
                message.what  = commonData.SHOW_TIP_CODE;
                message.obj = commonData.SERVER_ERROR;
                handler.sendMessage(message);
            }

     }catch (Exception e){
         Message msg = new Message();
         msg.what = commonData.DATA_ERROR_CODE;
         msg.obj = commonData.DATA_ERROR;
         handler.sendMessage(msg);

         LogToSd.debug("查找好友服务器返回成功，但是解析失败：可能原因json解析失败");
         LogToSd.debug(e.getMessage());
         LogToSd.debug(response.toString());
         e.getStackTrace();
     }

 }





  //添加好友或者加入社交圈成功
  public static void addFriendOrCircleSuccess(Context context,JSONObject response,final Handler handler, Object object){
        try{
            Message msg = new Message();
            String status = response.getString("status");
            Map map = new HashMap<String,Object>();
            if(status.equals("1")){              //添加好友成功


                Log.e("=============添加好友成功========！！！！！","");

                //保存入好友的数据库数据库
                msg.what = commonData.ADD_FRIEND_SUCCESS_CODE;

                if(User.getInstance()==null){
                    User.getUserInfo(context);
                }
                NewFriendPojo newFriendPojo;
                newFriendPojo = (NewFriendPojo) object;
                newFriendPojo.setStatus(NewFriendSqlHelper.PASS);


                getFriendListPojo pojo = new getFriendListPojo(newFriendPojo.getHeadImg(),newFriendPojo.getName(),newFriendPojo.getSex(),newFriendPojo.getUserId(),newFriendPojo.getUserAccount(),newFriendPojo.getSign(),newFriendPojo.getRole(),newFriendPojo.getReName());
                CircleUtil.saveFriend(pojo,context,User.getInstance().getId(), FriendListSqlHelper.FRIEND);






                //保存入new_friend数据库
                NewFriendUtil newFriendUtil = new NewFriendUtil(context, CommonFunc.stringToMD5(User.getInstance().getId()));
                newFriendUtil.updateStatus(NewFriendSqlHelper.PASS,newFriendPojo.getUserId());


                //提醒SocialCirlePagerHolder更新
                if(SocialCirlePagerHolder.socialCirlePagerHolder!=null){
                    Log.e("通知socialCirlePagerHolder更新","!!!!!!!");
                    List<getFriendListPojo> tList= new ArrayList<getFriendListPojo>();

                    tList.add(pojo);
                    tList.addAll(SocialCirlePagerHolder.socialCirlePagerHolder.f_list);
                    SocialCirlePagerHolder.socialCirlePagerHolder.f_list.clear();
                    SocialCirlePagerHolder.socialCirlePagerHolder.f_list.addAll(tList);
                }
                CircleUtil.isFriendChange = true;



                map.put("msg",commonData.ADD_FRIEND_SUCCESS);
                map.put("pojo",object);
                msg.obj = map;
                handler.sendMessage(msg);

            }else if(status.equals("3")){    //加入社交圈成功


                //保存入好友的数据库数据库
                if(User.getInstance()==null){
                    User.getUserInfo(context);
                }






                NewFriendPojo newFriendPojo;
                newFriendPojo = (NewFriendPojo) object;
                newFriendPojo.setStatus(NewFriendSqlHelper.PASS);


                //更改用户信息
                if (!newFriendPojo.getName().equals("")) {
                    User.getInstance().setBarberName(newFriendPojo.getName());
                }else{
                    User.getInstance().setBarberName(newFriendPojo.getUserAccount());
                }
                User.getInstance().setBarberId(newFriendPojo.getUserId());
                User.saveUserInfo(context);


                //保存入new_friend数据库
                NewFriendUtil newFriendUtil = new NewFriendUtil(context, CommonFunc.stringToMD5(User.getInstance().getId()));
                newFriendUtil.updateStatus(NewFriendSqlHelper.PASS,newFriendPojo.getUserId());


                //保存入friend_list本地数据库
                new FriendList().getFriendList(SocialListViewAdapter.getFriendListHandler,context);

                FileUtil.deleteFile(new File(FileUtil.detailCachePath));
                FileUtil.deleteFile(new File(FileUtil.storeActiveCachePathName));


                msg.what = commonData.ADD_CIRCLE_SUCCESS_CODE;
                map.put("msg",commonData.ADD_CIRCLE_SUCCESS);
                map.put("pojo",object);
                msg.obj = map;
                handler.sendMessage(msg);

            }else if(status.equals("4")){    //邀请对方加入社交圈成功

                //保存入好友的数据库数据库
                if(User.getInstance()==null){
                    User.getUserInfo(context);
                }
                NewFriendPojo newFriendPojo;
                newFriendPojo = (NewFriendPojo) object;
                newFriendPojo.setStatus(NewFriendSqlHelper.PASS);


                //保存入new_friend数据库
                NewFriendUtil newFriendUtil = new NewFriendUtil(context, CommonFunc.stringToMD5(User.getInstance().getId()));
                newFriendUtil.updateStatus(NewFriendSqlHelper.PASS,newFriendPojo.getUserId());


                //保存入friend_list本地数据库
                new FriendList().getFriendList(SocialListViewAdapter.getFriendListHandler,context);

                msg.what = commonData.INVITE_ADD_CIRCLE_SUCCESS_CODE;

                map.put("msg",commonData.INVITE_ADD_CIRCLE_SUCCESS);
                map.put("pojo",object);
                msg.obj = map;

                handler.sendMessage(msg);

            }else if(status.equals("2")){   //用户已存在社交圈
                msg.what = commonData.USER_CIRCLE_EXIST_CODE;
                map.put("msg",commonData.USER_CIRCLE_EXIST);
                map.put("pojo",object);
                msg.obj = map;
                handler.sendMessage(msg);

            }else if(status.equals("100")){ //用户未登录
                msg.arg1 = commonData.ADD_FRIEND_OR_CIRCLE_WITHOUT_LOGIN;
                msg.what = commonData.USE_NOT_LOGIN_CODE;

                map.put("msg",commonData.USER_NOT_LOGIN);
                map.put("pojo",object);
                msg.obj = map;
                handler.sendMessage(msg);
            }else if(status.equals("101")){ //参数错误
//                msg.what = commonData.PARAM_ERROR_CODE;
                msg.what  = commonData.SHOW_TIP_CODE;
                msg.obj = commonData.PARAM_ERROR;
                handler.sendMessage(msg);
            }else if(status.equals("102")){  //服务器端错误（关闭的服务器连接等）
                msg.what  = commonData.SHOW_TIP_CODE;
                msg.obj = commonData.SERVER_ERROR;
                handler.sendMessage(msg);
            }else if(status.equals("103")){//用户不存在
//                msg.what = commonData.USER_NO_EXIST_CODE;
                msg.what  = commonData.SHOW_TIP_CODE;
                msg.obj = commonData.USER_NO_EXIST;
                handler.sendMessage(msg);
            }else if(status.equals("5")){
//                msg.what = commonData.HAVE_BEEN_FRIEND_RELAX_CODE;
                msg.what  = commonData.SHOW_TIP_CODE;
                msg.obj = commonData.HAVE_BEEN_FRIEND_RELAX;
                handler.sendMessage(msg);
            }
            else{   //其他错误
                msg.what = commonData.USER_ADD_FRIEND_OR_CIRCLE_OTHER_ERROR_CODE;
                msg.obj = response.getString("msg");

                handler.sendMessage(msg);
            }



        }catch (Exception e){
            Log.e("=============解析错误=============",e.toString());
            Message msg = new Message();
            msg.what = commonData.DATA_ERROR_CODE;
            msg.obj = commonData.DATA_ERROR;
            handler.sendMessage(msg);
            LogToSd.debug(e.getMessage());

            LogToSd.debug(response.toString());
        }


  }


    //加入社交圈成功返回
    public static void addCircleSuccess(Context context,JSONObject response,final Handler handler, Object object) {
        try{
            Message msg = new Message();
            String status = response.getString("status");
            Map map = new HashMap<String,Object>();

            if(status.equals("1")){

                FileUtil.deleteFile(new File(FileUtil.detailCachePath));
                FileUtil.deleteFile(new File(FileUtil.storeActiveCachePathName));

                //保存入好友的数据库数据库
                if(User.getInstance()==null){
                    User.getUserInfo(context);
                }

                NewFriendPojo newFriendPojo;
                newFriendPojo = (NewFriendPojo) object;
                newFriendPojo.setStatus(NewFriendSqlHelper.PASS);

                //更改用户信息
                if (!newFriendPojo.getName().equals("")) {
                    User.getInstance().setBarberName(newFriendPojo.getName());
                }else{
                    User.getInstance().setBarberName(newFriendPojo.getUserAccount());
                }
                User.getInstance().setBarberId(newFriendPojo.getUserId());
                User.saveUserInfo(context);


                //保存入new_friend数据库
                NewFriendUtil newFriendUtil = new NewFriendUtil(context, CommonFunc.stringToMD5(User.getInstance().getId()));
                newFriendUtil.updateStatus(NewFriendSqlHelper.PASS,newFriendPojo.getUserId());


                //保存入friend_list本地数据库
                new FriendList().getFriendList(SocialListViewAdapter.getFriendListHandler,context);

                msg.what = commonData.ADD_CIRCLE_SUCCESS_CODE;

                map.put("msg", response.getString("msg"));
                map.put("pojo",object);
                msg.obj = map;
                handler.sendMessage(msg);
            }else if(status.equals("100")){ // 用户未登录
                msg.arg1 = commonData.ADD_CIRCLE_WITHOUT_LOGIN;
                msg.what = commonData.USE_NOT_LOGIN_CODE;
                map.put("msg",  commonData.USER_NOT_LOGIN);
                map.put("pojo",object);
                msg.obj = map;
                handler.sendMessage(msg);
            }else if(status.equals("101")){
//                msg.what = commonData.PARAM_ERROR_CODE;
                msg.what  = commonData.SHOW_TIP_CODE;
                msg.obj =  commonData.PARAM_ERROR;
                handler.sendMessage(msg);
            }else if(status.equals("102")){
//                msg.what = commonData.SERVER_ERROR_CODE;
                msg.what  = commonData.SHOW_TIP_CODE;
                msg.obj =  commonData.SERVER_ERROR;
                handler.sendMessage(msg);
            }else  if(status.equals("103")){
//                msg.what = commonData.USER_NO_EXIST_CODE;
                msg.what  = commonData.SHOW_TIP_CODE;
                msg.obj = commonData.USER_NO_EXIST;
                handler.sendMessage(msg);
            }else if(status.equals("104")){
//                msg.what = commonData.USER_NOT_CUSTOMER_CODE;
                msg.what  = commonData.SHOW_TIP_CODE;
                msg.obj = commonData.USER_NOT_CUSTOMER;
                handler.sendMessage(msg);
            }
            else {
                msg.what = commonData.USER_ADD_FRIEND_OR_CIRCLE_OTHER_ERROR_CODE;
                msg.obj = response.getString("msg");
                handler.sendMessage(msg);
            }
        }catch (Exception e){
            Message msg = new Message();
            msg.what = commonData.DATA_ERROR_CODE;
            msg.obj = commonData.DATA_ERROR;
            handler.sendMessage(msg);
            LogToSd.debug(e.getMessage());
            LogToSd.debug(response.toString());
            e.getStackTrace();


        }

    }



  public static  void addFriendSuccess(Context context,JSONObject response ,final  Handler handler, Object object){
      try{
          Message message = new Message();
          String status = response.getString("status");
          String msg =  response.optString("msg");
          Map map = new HashMap<String,Object>();
          if(status.equals("1")){
              Log.e("=============添加好友成功========！！！！！","");

              //保存入好友的数据库数据库
              message.what = commonData.ADD_FRIEND_SUCCESS_CODE;

              if(User.getInstance()==null){
                  User.getUserInfo(context);
              }
              NewFriendPojo newFriendPojo;
              newFriendPojo = (NewFriendPojo) object;
              newFriendPojo.setStatus(NewFriendSqlHelper.PASS);


              getFriendListPojo pojo = new getFriendListPojo(newFriendPojo.getHeadImg(),newFriendPojo.getName(),newFriendPojo.getSex(),newFriendPojo.getUserId(),newFriendPojo.getUserAccount(),newFriendPojo.getSign(),newFriendPojo.getRole(),newFriendPojo.getReName());
              CircleUtil.saveFriend(pojo, context, User.getInstance().getId(), FriendListSqlHelper.FRIEND);



              //保存入new_friend数据库
              NewFriendUtil newFriendUtil = new NewFriendUtil(context, CommonFunc.stringToMD5(User.getInstance().getId()));
              newFriendUtil.updateStatus(NewFriendSqlHelper.PASS,newFriendPojo.getUserId());


              //提醒更新
//              CircleUtil.isFriendChange = true;


              map.put("msg",commonData.ADD_FRIEND_SUCCESS);
              map.put("pojo",object);
              message.obj = map;
              handler.sendMessage(message);

          }else if(status.equals("2")){
//              message.what = commonData.HAVE_BEEN_FRIEND_RELAX_CODE;
              message.what  = commonData.SHOW_TIP_CODE;
              message.obj = commonData.HAVE_BEEN_FRIEND_RELAX;
              handler.sendMessage(message);
          }else if(status.equals("100")){
              message.arg1 = commonData.ADD_FRIEND_WITHOUT_LOGIN;
              message.what = commonData.USE_NOT_LOGIN_CODE;

              map.put("msg",commonData.USER_NOT_LOGIN);
              map.put("pojo",object);

              message.obj = map;
              handler.sendMessage(message);
          }else if(status.equals("101")){
//              message.what = commonData.PARAM_ERROR_CODE;
              message.what  = commonData.SHOW_TIP_CODE;
              message.obj = commonData.PARAM_ERROR;
              handler.sendMessage(message);
          }else if(status.equals("102")){
//              message.what = commonData.SERVER_ERROR_CODE;
              message.what  = commonData.SHOW_TIP_CODE;
              message.obj = commonData.SERVER_ERROR;
              handler.sendMessage(message);
          }
          else {
              message.what = commonData.USER_ADD_FRIEND_OR_CIRCLE_OTHER_ERROR_CODE;
              message.obj = msg;
              handler.sendMessage(message);
          }
      }catch (Exception e){
          Message msg = new Message();
          msg.what = commonData.DATA_ERROR_CODE;
          msg.obj = commonData.DATA_ERROR;
          handler.sendMessage(msg);
          LogToSd.debug("添加好友返回失败，可能原因：json解析失败");
          LogToSd.debug(e.getMessage());
          LogToSd.debug(response.toString());
          e.getStackTrace();

      }

  }



    /*发起好友请求的返回*/
    public static  void addFriendRequestSuccess(Context context,JSONObject response ,final  Handler handler, Object object){
        try{
            Log.e("=====发起请求====", "265");
            Message message = new Message();
            String status = response.getString("status");
            String msg =  response.optString("msg");
            if(status.equals("1")) {
                Log.e("=====发起请求====", "成功270");
                message.what = commonData.ADD_FRIEND_REQUEST_SUCCESS_CODE;
                message.obj = commonData.ADD_FRIEND_REQUEST_SUCCESS;
                handler.sendMessage(message);
            }else if(status.equals("2")){
                message.what = commonData.ADD_FRIEND_REQUEST_FAILED_CODE;
                message.obj = commonData.ADD_FRIEND_REQUEST_FAILED;
                handler.sendMessage(message);
            }else if(status.equals("102")){
                message.what = commonData.SERVER_ERROR_CODE;
                message.obj = commonData.SERVER_ERROR;
                handler.sendMessage(message);
            }else if(status.equals("103")){
                message.what = commonData.SERVER_ERROR_CODE;
                message.obj = commonData.SERVER_ERROR;
                handler.sendMessage(message);
            }else if(status.equals("107")){
                message.what = commonData.SHOW_TIP_CODE;
                message.obj = commonData.HAVE_BEEN_FRIEND_RELAX;
                handler.sendMessage(message);
            }

        }catch (Exception e){
            Message msg = new Message();
            msg.what = commonData.DATA_ERROR_CODE;
            msg.obj = commonData.DATA_ERROR;
            handler.sendMessage(msg);
            LogToSd.debug("发起添加好友的请求返回失败，可能原因：json解析失败");
            LogToSd.debug(e.getMessage());
            LogToSd.debug(response.toString());
            e.getStackTrace();

        }

    }


    //链接服务器失败
  public static void commonFail(Context context,JSONObject response ,final  Handler handler,Object object){
      try {
          LogToSd.debug("添加好友或者参加社交圈失败的信息");
          Message msg = new Message();
          msg.what = commonData.CAN_NOT_CONNECT_SERVER_CODE;
          msg.obj = commonData.CAN_NOT_CONNECT_SERVER;
          handler.sendMessage(msg);
          LogToSd.debug("添加好友或者参加社交圈失败的信息");
          LogToSd.debug(response.toString());
      }catch (Exception e){

          Message msg = new Message();
          msg.what = commonData.DATA_ERROR_CODE;
          msg.obj = commonData.DATA_ERROR;
          handler.sendMessage(msg);
          LogToSd.debug(e.getMessage());
          e.getStackTrace();
      }
  }




















}
